package local.blueheart.data.springdatademo.domain.system;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import java.util.ArrayList;
import java.util.List;

@Entity
public class AppRole extends AbstractDomainClass {
 
    private String role;

    public AppRole(String role) {
        this.role = role;
    }

    public AppRole(){

    }

    @ManyToMany(fetch = FetchType.LAZY,mappedBy = "appRoles")
    private List<AppUser> appUsers = new ArrayList<>();
 
    public String getRole() {
        return role;
    }
 
    public void setRole(String role) {
        this.role = role;
    }
 
    public List<AppUser> getAppUsers() {
        return appUsers;
    }
 
    public void setAppUsers(List<AppUser> appUsers) {
        this.appUsers = appUsers;
    }
 
    public void addUser(AppUser appUser){
        if(!this.appUsers.contains(appUser)){
            this.appUsers.add(appUser);
        }
 
        if(!appUser.getAppRoles().contains(this)){
            appUser.getAppRoles().add(this);
        }
    }
 
    public void removeUser(AppUser appUser){
        this.appUsers.remove(appUser);
        appUser.getAppRoles().remove(this);
    }
 
}